package kr.co.bettersoft.pushchat.adapter;

import java.util.List;

import kr.co.bettersoft.pushchat.activity.R;
import kr.co.bettersoft.pushchat.domain.PushChatDiary;
import kr.co.bettersoft.pushchat.fragment.DiaryListFragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * PushChatDiaryListAdapter
 * 다이어리 리스트 아답터 클래스
 *   썸네일 이미지  / 제목 / 글쓴이 닉네임 / 글쓴시각 / 좋아요 횟수 
 * @author blue
 *
 */
public class PushChatDiaryListAdapter extends BaseAdapter {
	String TAG = "PushChatDiaryListAdapter";
	private Context context;
	private final List<PushChatDiary> entries;
	private DiaryListFragment receiveDiaryListFragment;

	public PushChatDiaryListAdapter(Context context, DiaryListFragment diaryListFragment, List<PushChatDiary> entriesFn) {		
		this.context = context;
		this.entries = entriesFn;
		this.receiveDiaryListFragment = diaryListFragment;
	}
	Boolean blued = false;
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listView;
		if (convertView == null) {
			listView = new View(context);
			listView = inflater.inflate(R.layout.adapter_pushchat_diary_list, null);
		} else {
			listView = (View) convertView;
		}

		// set value into textview

		ImageView thumbnailImg = (ImageView) listView.findViewById(R.id.diary_thumbnail_img);
		TextView diarySubjectTxt = (TextView) listView.findViewById(R.id.diary_subject_txt);
		TextView diaryNickNameTxt = (TextView) listView.findViewById(R.id.diary_nick_name_txt);
		TextView diaryDateTxt = (TextView) listView.findViewById(R.id.diary_date_txt);
		TextView diaryLikeCountTxt = (TextView) listView.findViewById(R.id.diary_like_count_txt);


		if(((PushChatDiary)entries.get(position)).getThumbnail()==null){
			thumbnailImg.setImageDrawable(context.getResources().getDrawable(R.drawable.empty_diary));		// **	??
		}else{
			thumbnailImg.setImageBitmap(((PushChatDiary)entries.get(position)).getThumbnail());
		}
		diarySubjectTxt.setText(((PushChatDiary)entries.get(position)).getSubject());
		diaryNickNameTxt.setText(((PushChatDiary)entries.get(position)).getNickName());
		diaryDateTxt.setText(((PushChatDiary)entries.get(position)).getRegisterDate());
		diaryLikeCountTxt.setText(((PushChatDiary)entries.get(position)).getLikeCount());

		// 삭제 버튼
		View delete_line_layout = listView.findViewById(R.id.delete_line_layout);
		Button delete_line_btn = (Button) listView.findViewById(R.id.delete_line_btn);
		delete_line_btn.setTag(position);
		if(((PushChatDiary)entries.get(position)).isShowCancelBtnYn()){
			delete_line_layout.setVisibility(View.VISIBLE);
		}else{
			delete_line_layout.setVisibility(View.GONE);
		}
		delete_line_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				receiveDiaryListFragment.cancelPosition = Integer.parseInt(v.getTag()+"");
				receiveDiaryListFragment.confirmAndDelete();
			}
		});
		//		memberIdTxt.setText(((PushChatDiary)entries.get(position)).getNickName()+"("+((PushChatDiary)entries.get(position)).getMemberId()+")");
		//		memberCommentTxt.setText(((PushChatDiary)entries.get(position)).getComment());
		////		TextView textView1 = (TextView) listView.findViewById(R.id.chat_member_list_nickname);
		////		textView1.setText(((PushChatDiary)entries.get(position)).getNickName()+"("+((PushChatDiary)entries.get(position)).getMemberId()+")");

		return listView;
	}

	@Override
	public int getCount() {
		return entries.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

}